package main.ui.tabs;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseListener;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import main.parsers.clubs.Club;
import main.parsers.clubs.ClubParser;
import main.parsers.utils.Utils;
import main.ui.actions.ClubMouseListener;

public class ClubsTab extends JPanel 
{
	private static final long serialVersionUID = 1828866712821234701L;
	
	private ClubParser cp = ClubParser.getInstance();
	
	public ClubsTab()
	{
		super(new FlowLayout(FlowLayout.LEFT, 8, 8));
		 
		for (Entry<String, Integer> side: Utils.loadSides(false).entrySet())
		{
			add(createSide(side.getKey(), side.getValue()));
		}
	}
	
	private JPanel createSide(String side, int id)
	{
		JPanel panel = new JPanel();
		
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), side));
		
		for (Entry<String, Integer> division: Utils.loadDivisions(id).entrySet())
		{
			panel.add(createDivision(division.getKey(), division.getValue()));
		}
		return panel;
	}
	
	private JPanel createDivision(String division, int id)
	{
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 8));
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), division));
		
		MouseListener listener = new ClubMouseListener();
		
		for (final Club club: cp.search(id))
		{
			JLabel logo = new JLabel(club.getImage())
			{
				private static final long serialVersionUID = 7319061002273296986L;

				public int hashCode()
				{
					return club.getId();
				}
				
				public String toString()
				{
					return club.getName();
				}
			};
			
			logo.setToolTipText(club.getName());
			logo.addMouseListener(listener);	
			logo.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
			
			panel.add(logo);
		}
		panel.setPreferredSize(new Dimension(700, 150));
		return panel;
	}
}
